﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DiaosFix.Web.MVC.Models;

namespace DiaosFix.Web.MVC.Controllers
{
    public class RequestController : Controller
    {
        //
        // GET: /Request/
        private DiaosFix.Core.RequestsOperator ro = new Core.RequestsOperator();

        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult AllRequests()
        {
            var roResponse = ro.FindAllRequest();
            if (roResponse.IsSuccess)
            {
                return View(roResponse.Item);
            }
            else
            {
                ViewBag.ErrorMessage = roResponse.Message;
            }
            return View();
        }

        [Authorize]
        public ActionResult Raise()
        {
            var objModelWrapper = GetDefaultRaiseRequestModelWrapper();
            if (null != objModelWrapper)
            {
                return View(objModelWrapper);
            }
            else
            {
                ViewBag.ErrorMessage = "Error Getting Model Wrapper";
            }

            return View();
        } 

        [Authorize]
        [HttpPost]
        public ActionResult Raise(RaiseRequestModelWrapper modelWrapper)
        {
            if (ModelState.IsValid)
            {
                var roResponse = ro.RaiseRequest(User.Identity.Name, Convert.ToInt32(modelWrapper.PostModel.RequestAreaName), modelWrapper.PostModel.RequestDescription);
                if (roResponse.IsSuccess)
                {
                    return RedirectToAction("RaiseRequestSuccess", new { requestId = roResponse.Item.Id });
                }
                else
                {
                    ModelState.AddModelError("", roResponse.Message);
                }
            }
            return View(GetDefaultRaiseRequestModelWrapper());
        }

        public ActionResult RaiseRequestSuccess(int requestId)
        {
            var roResponse = ro.FindRequest(requestId);
            if (roResponse.IsSuccess)
            {
                return View(roResponse.Item);    
            }
            else
            {
                ViewBag.ErrorMessage = roResponse.Message;
            }
            return View();
        }

        [Authorize]
        public ActionResult MyOpenRequests()
        {
            var userResponse = ro.FindAllRequestsRaisedBy(User.Identity.Name);
            if (userResponse.IsSuccess)
            {
                return View(userResponse.Item.Where(p => p.RequestStatus.StatusName != Core.PubConstant.RequestStatus.Closed.ToString()).ToList());
            }
            else
            {
                ViewBag.ErrorMessage = userResponse.Message;
            }
            return View();
        }

        [Authorize]
        public ActionResult MyClosedRequests()
        {
            var userResponse = ro.FindAllRequestsRaisedBy(User.Identity.Name);
            if (userResponse.IsSuccess)
            {
                return View(userResponse.Item.Where(p => p.RequestStatus.StatusName == Core.PubConstant.RequestStatus.Closed.ToString()).ToList());
            }
            else
            {
                ViewBag.ErrorMessage = userResponse.Message;
            }
            return View();
        }

        [Authorize]
        public ActionResult AssignedToMe()
        {
            var roResponse = ro.FindAllRequestsAssignedTo(User.Identity.Name);
            if (roResponse.IsSuccess)
            {
                return View(roResponse.Item);
            }
            else
            {
                ViewBag.ErrorMessage = roResponse.Message;
            }
            return View();
        }

        private RaiseRequestModelWrapper GetDefaultRaiseRequestModelWrapper()
        {
            var uao = new DiaosFix.Core.RequestAreaOperator();
            var areaListResponse = uao.FindAllRequestAreas();

            if (areaListResponse.IsSuccess)
            {
                IEnumerable<SelectListItem> selectList =
                from c in areaListResponse.Item
                select new SelectListItem
                {
                    Text = c.AreaName,
                    Value = c.Id.ToString()
                };

                var modelWrapper = new RaiseRequestModelWrapper()
                {
                    GetModel = selectList,
                    PostModel = new RaiseRequestModel()
                };

                return modelWrapper;
            }
            return null;
        }

    }
}
